package org.neurone.application.impl;

import java.util.List;

import org.neurone.application.ApplicationFace;
import org.neurone.domain.Application;
import org.neurone.domain.alter.ApplicationAlter;
import org.neurone.userspace.dao.ApplicationDao;
import org.neurone.util.criteria.Criteria;
import org.neurone.util.criteria.CriteriaOperator;
import org.neurone.util.criteria.impl.CriteriaKeyValue;

public class ApplicationFaceLocal extends ApplicationAlter implements ApplicationFace {
	private ApplicationDao applicationDao;
	protected boolean persistentAvailable;
	
	@Override
	public Long getApplicationID() {
		return applicationID;
	}
	@Override
	public String getApplicationKey() {
		return applicationKey;
	}
	@Override
	public String getApplicationName() {
		return applicationName;
	}
	@Override
	public String getApplicationDescription() {
		return applicationDescription;
	}
	@Override
	public String getCallbackUrl() {
		return callbackUrl;
	}
	@Override
	public String getApplicationType() {
		return applicationType;
	}
	@Override
	public String getPluginType() {
		return pluginType;
	}
	@Override
	public String getServerIP() {
		return serverIP;
	}
	@Override
	public String getIcon() {
		return icon;
	}
	@Override
	public String getVisibleLevelKey() {
		return visibleLevelKey;
	}
	@Override
	public String getTags() {
		return tags;
	}
	public void setApplicationID(Long applicationID) {
		this.applicationID = applicationID;
	}
	public void setApplicationKey(String applicationKey) {
		this.applicationKey = applicationKey;
	}
	public void setApplicationName(String applicationName) {
		this.applicationName = applicationName;
	}
	public void setApplicationDescription(String applicationDescription) {
		this.applicationDescription = applicationDescription;
	}
	public void setCallbackUrl(String callbackUrl) {
		this.callbackUrl = callbackUrl;
	}
	public void setApplicationType(String applicationType) {
		this.applicationType = applicationType;
	}
	public void setPluginType(String pluginType) {
		this.pluginType = pluginType;
	}
	public void setServerIP(String serverIP) {
		this.serverIP = serverIP;
	}
	public void setIcon(String icon) {
		this.icon = icon;
	}
	public void setVisibleLevelKey(String visibleLevelKey) {
		this.visibleLevelKey = visibleLevelKey;
	}
	public void setTags(String tags) {
		this.tags = tags;
	}
	
	protected ApplicationFaceLocal() {
		
	}
	
	@Override
	public String getEntryURL(String targetURL, String newToken) {
		return null;
	}
	@Override
	public void flush() {
		this.applicationDao.update(this);
	}
	@Override
	public boolean isPersistentAvailable() {
		return this.persistentAvailable;
	}
	protected void initByApplicationKey(String applicationKey) {
		Criteria terms = new CriteriaKeyValue("applicationKey", CriteriaOperator.EQUALS, applicationKey);
		List<Application> applicationList = this.applicationDao.search(terms, 0, 0);
		if(applicationList.size() >0)
		{
			initApplication(applicationList.get(0));
		}
		else
		{
			initApplication(null);
		}
	}
	protected void initByApplicationID(Long	applicationID) {
		
		initApplication(this.applicationDao.find(applicationID));
	}
	private void initApplication(Application application) {
		this.persistentAvailable = application != null;
		if(this.persistentAvailable)
		{
			this.applicationID = application.getApplicationID();
			this.setApplicationDescription(application.getApplicationDescription());
			this.setApplicationKey(application.getApplicationKey());
			this.setApplicationName(application.getApplicationName());
			this.setApplicationType(application.getApplicationType());
			this.setCallbackUrl(application.getCallbackUrl());
			this.setIcon(application.getIcon());
			this.setPluginType(application.getPluginType());
			this.setServerIP(application.getServerIP());
			this.setTags(application.getTags());
			this.setVisibleLevelKey(application.getVisibleLevelKey());
		}
		
	}
	public ApplicationDao getApplicationDao() {
		return applicationDao;
	}
	protected void setApplicationDao(ApplicationDao applicationDao) {
		this.applicationDao = applicationDao;
	}

}
